<?php
/**
 * 会员数据处理控制器
 */
namespace app\manage\controller\member;
use app\manage\controller\Base;

class Handle extends Base
{
    private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

	/**
	 * 添加会员
	 */
    public function add(){
    	$memberData = $this->params;

    	$result = $this->validate($memberData, 'Member.manage_add');
    	if(true !== $result) {
    	    $this->error($result);
        }
        $model = model('member');
        $result = $model->addMember($memberData);

        if(false===$result) {
            $this->error($model->getError());
        }
        if(true===$result){
            $this->success('空单会员添加成功');
        }
        $scoreId = $result;

        $this->success('添加成功',null,['callback'=>url('bonus.api/deal_bonus'),'operate_id'=>$scoreId]);
    }

    /**
     * 删除会员
     * @return [type] [description]
     */
    public function del(){

        $this->error('系统不支持删除用户');
        
    }

    /**
     * 编辑会员
     * @return [type] [description]
     */
    public function edit(){
        $params = $this->params;
        $validate = $this->validate($params, 'Member.manage_edit');
        if(true!==$validate){
            $this->error($validate);
        }
        $id = $params['id'];
        $data = [
            'mname' => $params['mname'],
            'city' => isset($params['city'])?$params['city']:'',
            'address' => isset($params['address'])?$params['address']:'',
            'postcode' => isset($params['postcode'])?$params['postcode']:'',
            'idcard' => isset($params['idcard'])?$params['idcard']:'',
            'remark' => isset($params['remark'])?$params['remark']:'',
            'is_world' => isset($params['is_world'])?$params['is_world']:0,
            'is_share' => isset($params['is_share'])?$params['is_share']:0,
            //'is_agent' => isset($params['is_agent'])?$params['is_agent']:0,
        ];

        if(model('member')->save($data,['id'=>$id])){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }

    /**
     * 修改密码
     * @return [type] [description]
     */
    public function password(){
        $params = $this->params;
        $uid = isset($params['id'])?$params['id']:'';
        $password = isset($params['password'])?$params['password']:'';
        $safe_password = isset($params['safe_password'])?$params['safe_password']:'';
        if(!$id){
            $this->error('请选择要修改的对象');
        }
        if(!$password&&!$safe_password){
            $this->error('未做任何修改');
        }

        if($password){
            $hash = hashPwd($password);
            model('member')->save(['password'=>$hash],['id'=>$uid]);
        }
        if($safe_password){
            $hash = hashPwd($safe_password);
            model('member')->save(['safe_password'=>$hash],['id'=>$uid]);
        }
        $this->success('操作成功');
    }

    /**
     * 禁用会员
     * @return [type] [description]
     */
    public function disable(){
        $params = $this->params;
        $ids = isset($params['data'])?$params['data']:[];
        if(empty($ids)){
            $this->error('未选择任何操作对象');
        }
        foreach($ids as $uid):
            model('member')->save(['status'=>3],['id'=>$uid]);
        endforeach;
        $this -> success('操作完成');
    }

    /**
     * 冻结会员
     * @return [type] [description]
     */
    public function freeze(){
        $params = $this->params;
        $ids = isset($params['data'])?$params['data']:[];
        if(empty($ids)){
            $this->error('未选择任何操作对象');
        }
        $datas = [];
        foreach($ids as $uid):
            model('member')->save(['status'=>4],['id'=>$uid]);
        endforeach;
        $this -> success('操作完成');
    }

}
